/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.hello.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxp.springboot.hello.mapper.UserMapper;
import com.zxp.springboot.hello.model.Employee;
import com.zxp.springboot.hello.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author: shollin
 * @date: 2021/6/27/027 17:28
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    UserMapper userMapper;

    @GetMapping("/user")
    public User get(Long id){
        User user= userMapper.getById(id);
        return user;
    }

    @GetMapping("/user/page")
    public PageInfo<User> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "2") Integer rows) {

        PageHelper.startPage(page, rows);
        List<User> employees = userMapper.selectAll();
        return new PageInfo<>(employees);
    }

    @PostMapping("/user/save")
    public void save(@RequestBody User user){
        int rows = userMapper.insert(user);
        log.debug("保存成功：{}",user.getId());
    }

    @PostMapping("/user/saveBatch")
    public Integer insertUserBatch() {
        int rows = 10;
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setEmail(RandomUtil.randomNumbers(6) + "@qq.com");
            user.setName( RandomUtil.randomString("中华人民共国万岁毛主赵钱孙李之初多少钱啊",3) );
            user.setSalary(new BigDecimal(RandomUtil.randomDouble(3000, 50000, 2, RoundingMode.HALF_UP)));
            user.setUsername(IdUtil.fastSimpleUUID());

            user.setCreateTime(RandomUtil.randomDay(-365 * 5, 365 * 5));
            user.setUpdateTime(LocalDateTime.now());

            int insert = userMapper.insert(user);
        }
        return rows;
    }

}
